<?php
defined('IN_YuLin') || exit('NO PERMIT!');
UserLogin(U($m.'/'.$c.'/'.$a));
$pageShow = 'member/lowerReport';
// 获取所有的下级信息
$userLineData = array();
getUserLine($_uid, $userLineData);
$uidStr       = implode(',', array_keys($userLineData));
$userTab      = Table('user');
$userinfoTab  = Table('user_info');
$timeLine     = NoBadStr($_REQUEST['timeline']);

$where           = ' WHERE id IN(' . $uidStr . ') ';
$orderPageWhere  = ' WHERE uid IN(' . $uidStr . ') ';
$rpLogWhere      = ' WHERE uid IN(' . $uidStr . ') ';
if ($timeLine == "1"){
    // 昨日
//     $where          .= ' AND u.reg_time > ' . strtotime(date('Ymd', strtotime('-1 day'))) . ' AND u.reg_time < ' . strtotime(date('Ymd'));
    $orderPageWhere .= ' AND createtime > ' . strtotime(date('Ymd', strtotime('-1 day'))) . ' AND createtime < ' . strtotime(date('Ymd'));
    $rpLogWhere     .= ' AND dateline > ' . strtotime(date('Ymd', strtotime('-1 day'))) . ' AND dateline < ' . strtotime(date('Ymd'));
}elseif ($timeLine == "2"){
    // 本月
//     $where          .= ' AND u.reg_time > ' . strtotime(date('Ym') . '01');
    $orderPageWhere .= ' AND createtime > ' . strtotime(date('Ym') . '01');
    $rpLogWhere     .= ' AND dateline > ' . strtotime(date('Ym') . '01');
}elseif ($timeLine == "3"){
    // 上个月
//     $where          .= ' AND u.reg_time > ' . strtotime(date('Ym', strtotime('-1 month')) . '01') . ' AND u.reg_time < ' . strtotime(date('Ym') . '01');
    $orderPageWhere .= ' AND createtime > ' . strtotime(date('Ym', strtotime('-1 month')) . '01') . ' AND createtime < ' . strtotime(date('Ym') . '01');
    $rpLogWhere     .= ' AND dateline > ' . strtotime(date('Ym', strtotime('-1 month')) . '01') . ' AND dateline < ' . strtotime(date('Ym') . '01');
}else{
    // 今天
//     $where          .= ' AND u.reg_time > ' . strtotime(date('Ymd'));
    $orderPageWhere .= ' AND createtime > ' . strtotime(date('Ymd'));
    $rpLogWhere     .= ' AND dateline > ' . strtotime(date('Ymd'));
}
// 先查所有用户信息， 再查这些用户的投注信息，  将投注信息按用户整理，按代理整理
$sql = 'SELECT COUNT(id) as cnt FROM ' . $userTab . ' AS u ' . $where;
$tmp = $db->getrow($sql);
if (!$tmp['cnt']){
    $tpl->display($pageShow);
    return;
}

$sql = 'SELECT u.`id`, u.`username`, info.`user_type`, info.`lower_nums` 
        FROM ' . $userTab . ' AS u
        LEFT JOIN ' . $userinfoTab . ' AS info
        ON u.`id` = info.`uid`
        ' . $where;
$page     = max(1, intval($_GET['page']));
$size     = 10;
$limit    = ' LIMIT '.($page - 1) * $size.','.$size;
$url      = '?m='.$m.'&c='.$c;
$orderby  = ' ORDER BY u.id DESC ';
$data     = $db->getall($sql . $orderby.$limit);
$multi    = Multi($url,$page,$tmp['p'],$size);

// 准备用户的其他统计信息
$uidStr   = implode(',', array_column($data, 'id'));
$mainTab  = Table('lottery_order_main');
$sql      = 'SELECT id, amount, prize, uid FROM ' . $mainTab . $orderPageWhere;
$orderData= $db->getall($sql);
if ($orderData){
    // 查询这些订单的返点信息
    $rpLogTab   = Table('repoint_log');
    $sql        = 'SELECT uid, order_id, order_amount, rps_amount FROM ' . $rpLogTab . ' WHERE order_id IN (' . implode(',', array_column($orderData, 'id')) . ')';
    $rpLogData  = $db->getall($sql);
    $rpLogIdKey = array();
    foreach ($rpLogData as $k => $v){
        $rpLogIdKey[$v['uid']][$v['order_id']] = $v;
    }
}

// 计算下级的投注人数与总投注金额
foreach ($data as $k => $v){
    if ($v['user_type'] != '1') {
        foreach ($orderData as $ok => $ov){
            if ($ov['uid'] == $v['id']){
                $data[$k]['betAmount']   = $ov['amount'];
                $data[$k]['prizeAmount'] = $ov['prize'];
                $data[$k]['rpsAmount']   = 0;
            }
        }
        $data[$k]['betUserNums'] = '1';
        continue;
    }
    $userLineData   = array();
    getUserLine($v['id'], $userLineData);
    if (!$userLineData) continue;
    // 拿到当前用户的下级代理以及玩家id信息
    $lowerUserData  = array_keys($userLineData);
    $tmpBetUserNums = array();
    foreach ($orderData as $ok => $ov){
        if (in_array($ov['uid'], $lowerUserData) || $ov['uid'] == $v['id']){
            if (!in_array($ov['uid'], $tmpBetUserNums)) array_push($tmpBetUserNums, $ov['uid']);
            $data[$k]['betAmount']   += $ov['amount'];
            $data[$k]['prizeAmount'] += $ov['prize'];
            $data[$k]['rpsAmount']   += $rpLogIdKey[$v['id']][$ov['id']]['rps_amount'];
        }
    }
    $data[$k]['betUserNums'] = count($tmpBetUserNums);
}
$tpl->display($pageShow);
